package course.myra.testset;

import java.util.List;

import done.myra.excercise.ch9.Test01;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

/*
 * 比较器test
 * 6.用三种方法做对象的比较排序
* -1写比较器类继承compartor类、调用工具类进行排序
* --Collections.sort(lst, new StudentComparator());
* -2待比较的类中实现comparable接口中的compareTo方法、在调用工具类进行排序
* --Collections.sort(lst);
* -3需要对对象排序时写匿名内部类
* --Collections.sort(lst, new Comparator<Student>(){
* @override
* }
 */
public class TestStuComp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Student继承comparable接口
		// test01();
		// 构造studentComparator类
		// test02();
		// 匿名内部类实现按照姓名排序
		test03();

	}

	/**
	 * 匿名内部类实现comparator接口
	 */
	private static void test03() {
		Student stu1 = new Student("赵四", "1099", 90);
		Student stu2 = new Student("wanglu", "00110", 99);
		Student stu3 = new Student("zhaoliu", "10110", 79);
		Student stu4 = new Student("yule", "20110", 60);
		Student stu5 = new Student("-乐游", "9011", 89);
		Student stu6 = new Student("wangLu", "201901", 59);

		List<Student> stuList = new ArrayList<>();
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);
		stuList.add(stu4);
		stuList.add(stu1);
		stuList.add(stu5);
		stuList.add(stu6);

		// 按照姓名排序
		Collections.sort(stuList, new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				// 比较大小

				return o1.getStuName().compareTo(o2.getStuName());
			}

		});

		Iterator<Student> it = stuList.iterator();
		while (it.hasNext()) {
			Student stuTemp = it.next();
			stuTemp.printStuMes();
		}
	}

	/**
	 * StudentComparator继承comparator接口
	 */
	private static void test02() {
		Student stu1 = new Student("赵四", "1099", 90);
		Student stu2 = new Student("wangwu", "00110", 99);
		Student stu3 = new Student("zhaoliu", "10110", 79);
		Student stu4 = new Student("yule", "20110", 60);
		Student stu5 = new Student("-乐游", "9011", 89);

		List<Student> stuList = new ArrayList<>();
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);
		stuList.add(stu4);
		stuList.add(stu1);
		stuList.add(stu5);

		Collections.sort(stuList, new StudentComparator());

		Iterator<Student> it = stuList.iterator();
		while (it.hasNext()) {
			Student stuTemp = it.next();
			stuTemp.printStuMes();
		}
	}

	/**
	 * Student继承comparable接口
	 */
	private static void test01() {
		Student stu1 = new Student("赵四", "1099", 90);
		Student stu2 = new Student("wangwu", "00110", 99);
		Student stu3 = new Student("zhaoliu", "10110", 79);
		Student stu4 = new Student("yule", "20110", 60);

		List<Student> stuList = new ArrayList<>();
		stuList.add(stu1);
		stuList.add(stu2);
		stuList.add(stu3);
		stuList.add(stu4);
		stuList.add(stu1);

		Collections.sort(stuList);

		Iterator<Student> it = stuList.iterator();
		while (it.hasNext()) {
			Student stuTemp = it.next();
			stuTemp.printStuMes();
		}
	}

}
